/**
 * 队列
 */
export class Queue<T>{
    private _lowestCount:number = 0;
    private _count:number = 0;
    private _items:{[key:number]:T} = {};
    public constructor(){

    }
    /**
     * 入队
     * @param item 
     */
    public enqueue(item:T):void{
        this._items[this._count] = item;
        this._count++;
    }
    /**
     * 出队列
     * @returns 
     */
    public dequeue():T{
        if(this.isEmpty()){
            return null;
        }
        let result = this._items[this._lowestCount];
        delete this._items[this._lowestCount];
        this._lowestCount++;
        return result
    }
    /**
     * 返回队列头
     * @returns 
     */
    public peek():T{
        if(this.isEmpty()){
            return null;
        }
        return this._items[this._lowestCount];
    }
    /**
     * 返回队列长度
     * @returns 
     */
    public size():number{
        return this._count - this._lowestCount;
    }
    public isEmpty(){
        return this.size() == 0;
    }
    public clear(){
        this._count = 0;
        this._lowestCount = 0;
        this._items = {};
    }
    public toString():string{
        if(this.isEmpty()){
            return "";
        }
        let str = "";
        for(let i = this._lowestCount;i<this._count;i++){
            str+=`${i},${this._items[i]} `
        }
        return str;
    }
}